/*
 * Copyright 2010-2011 WebEqua Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.mathcloud.math.core.maple.operator.algebra;


import org.mathcloud.math.core.maple.factory.MapleQuantityObjectFactory;
import org.mathcloud.math.core.maple.manager.MapleSupervisor;
import org.mathcloud.math.core.maple.operator.MapleBinaryOperator;
import org.mathcloud.math.core.maple.quantity.MapleQuantityObject;
import org.mathcloud.math.core.maple.quantity.MapleRealNumber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.mathcloud.math.core.common.operator.algebra.Log;
import org.mathcloud.math.core.common.signature.QuantityObjectSignature;


public class MapleLog extends MapleBinaryOperator implements Log
{
	public MapleLog()
	{
		super();
		signature.setMMLContentTag("log");
	}

	@Override
	public void calculate()
	{
		try {
			if(operand2 == null) operand2 = new MapleRealNumber(10);
			
			if(operand2.getSignature().isMatrix()) 
				throw new Exception("operand2 is Matrix, the current situation isn't supported!");
			
			QuantityObjectSignature sign = getResultSignature();
			if (sign!=null) {
		     
				String MapleJob;
				
				log.debug("Generate variable 'operand1' in Maple");
				MapleQuantityObject tOp1 = MapleQuantityObjectFactory.getInstance(operand1);
				log.debug("Generate variable 'operand2' in Maple");
				MapleQuantityObject tOp2 = MapleQuantityObjectFactory.getInstance(operand2);
				log.debug("Generate 'log'-funtion in Maple form");
				MapleJob = MapleQuantityObject.getEvaluateForm("map", new Object[] {"log["+tOp2.getVarName()+"]",tOp1.getVarName()}) + "evalhf(%):";
				log.debug("Maple form is {}", MapleJob);
				res = MapleQuantityObjectFactory.getInstance(sign, MapleSupervisor.getMapleEngine().evaluate(MapleJob));
		  } 
			else {
				log.warn("ResultSignature isn't defined");
			}
		}
		catch(Exception e) {
			log.error("Exception:", e);
		}
	}	

	private static final Logger log = LoggerFactory.getLogger(MapleLog.class);
}
